package com.jjh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jjh.consts.Result;
import com.jjh.mapper.CategoryMapper;
import com.jjh.pojo.Category;
import com.jjh.pojo.User;
import com.jjh.service.CateGoryService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class CateGoryServiceImpl implements CateGoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 条件分页查询
     * @param page
     * @param size
     * @param map
     * @return
     */
    @Override
    public Result page(Integer page, Integer size, Map<String, Object> map) {
        PageHelper.startPage(page,size);
        List<Category> categories = getByCondition(map);
        PageInfo<Category> pageInfo = new PageInfo<>(categories);
        return Result.ok(pageInfo);
    }

    @Override
    public List<Category> getByCondition(Map<String, Object> map) {
        return categoryMapper.getByCondition(map);
    }

    /**
     * 添加分类
     * @param category
     * @return
     */
    @Override
    public Result addOne(Category category) {
        //先获取分类名称
        String name = category.getName();
        //先查询一下看看是否已经有了
        Category category1 =categoryMapper.getByName(name);
        if(!ObjectUtils.isEmpty(category1)){
            return Result.error("该分类已经存在");
        }
        int row = categoryMapper.insert(category);
        if(row>0){
            return Result.ok("添加成功");
        }else {
            log.error("添加分类失败:{}",category);
            return Result.error("添加分类失败!");
        }
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @Override
    public Result updateOne(Category category) {
        //先查询该分类是否存在
        Category cateGory = categoryMapper.getByName(category.getName());
        if(!ObjectUtils.isEmpty(cateGory)&&!Objects.equals(cateGory.getId(), category.getId())){
            //要修改的和自己不是一个对象，则返回错误
            return Result.error("该分类已经存在，请重新修改!");
        }
        int row = categoryMapper.updateByPrimaryKey(category);
        if(row>0){
            return Result.ok("修改成功");
        }else{
            log.error("修改分类失败:{}",category);
            return Result.error("修改失败！");
        }
    }

    /**
     * 批量删除分类
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(List<Integer> ids) throws SQLIntegrityConstraintViolationException {
         categoryMapper.deleteByIds(ids);
         return Result.ok("删除成功");
    }

    /**
     * 获取所有的菜品分类
     * @return
     */
    @Override
    public Result getDishCates() {
        //因为只要两个分类，所以菜品分类类型的id为1，
        Integer dishTypeId = 1;
        List<Category> categoryList = categoryMapper.getCatesByTypeId(dishTypeId);

        return Result.ok(categoryList);
    }

    @Override
    public Result getTaoCanCates() {
        //因为只要两个分类，所以套餐分类类型的id为2，
        Integer dishTypeId = 2;
        List<Category> categoryList = categoryMapper.getCatesByTypeId(dishTypeId);

        return Result.ok(categoryList);
    }


}
